package com.internetCafes.spms.web.customer.controller;


import com.internetCafes.spms.common.module.excel.ExcelUtils;
import com.internetCafes.spms.common.utils.entity.RR;
import com.internetCafes.spms.common.utils.entity.page.PageItemDTO;
import com.internetCafes.spms.common.utils.entity.page.PageItemVO;
import com.internetCafes.spms.core.annotation.SysLog;
import com.internetCafes.spms.web.customer.bizservice.area.IEntAreaBizService;
import com.internetCafes.spms.web.customer.common.cont.AreaConst;
import com.internetCafes.spms.web.customer.model.area.*;
import com.internetCafes.spms.web.customer.model.user.EntUserAreaPagingReq;
import com.sms.common.util.hutool.core.bean.BeanUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.internetCafes.spms.common.base.BaseWebController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 * 区域表 前端控制器
 * </p>
 *
 * @author Zj
 * @since 2021-10-18
 */
@RestController
@RequestMapping("/ent/area")
public class EntAreaController extends BaseWebController {

    @Autowired
    private IEntAreaBizService entAreaBizService;

    /**
     * 获取区域详情
     */
    @GetMapping("/detail/{id}")
    public RR<EntAreaDetailForSaleRes> detail(@PathVariable("id") Long id) {
        return RR.success(entAreaBizService.detailEntAreaSaleDetailByAreaId(id));
    }

    /**
     * 区域列表导出
     * @param req 请求参数
     * @param response 请求
     */
    @PostMapping("/export")
    @RequiresPermissions("ent:area:export")
    public void export(@RequestBody EntAreaExcelReq req, HttpServletResponse response) {
        List<EntAreaExcel> entAreaExcels = entAreaBizService.areaExcel(req);
        ExcelUtils.exportExcel(entAreaExcels, "区域管理", "区域管理", EntAreaExcel.class, "area.xls", response);
    }

    /**
     * 区域名单管导出
     */
    @PostMapping("/user/export")
    @RequiresPermissions("ent:area:export")
    public void export(@RequestBody EntAreaExportReq req, HttpServletResponse response) {
        if (AreaConst.EntAreaExportTypeEnum.AREA_USER.getId().equals(req.getTypeId())) {
//            List<EntAreaUserExcel> resultList = entAreaBizService.entAreaUserExcelList(new EntAreaUserPagingReq());
            EntAreaUserPagingReq pagingReq = new EntAreaUserPagingReq();
            BeanUtil.copyProperties(req,pagingReq);
            List<EntAreaUserExcel> resultList = entAreaBizService.entAreaUserExcelList(pagingReq);
            ExcelUtils.exportExcel(resultList, "区域名单", "区域名单", EntAreaUserExcel.class, "areaUser.xls", response);
        } else {
//            List<EntUserAreaExcel> entUserAreaExcels = entAreaBizService.entUserAreaExcelList(new EntUserAreaPagingReq());
            EntUserAreaPagingReq pagingReq = new EntUserAreaPagingReq();
            BeanUtil.copyProperties(req,pagingReq);
            List<EntUserAreaExcel> entUserAreaExcels = entAreaBizService.entUserAreaExcelList(pagingReq);
            ExcelUtils.exportExcel(entUserAreaExcels, "区域名单", "区域名单", EntUserAreaExcel.class, "userArea.xls", response);
        }
    }

    /**
     * 详情-区域角度
     */
    @GetMapping("/user/detail/{id}")
    @RequiresPermissions("ent:area:detail")
    public RR<EntAreaUserDetailRes> userDetail(@PathVariable Long id) {
        return RR.success(entAreaBizService.entAreaUserDetail(id));
    }

    /**
     * 区域名单-分页-区域角度
     */
    @PostMapping("/user/paging")
    @RequiresPermissions("ent:area:paging")
    public RR<PageItemVO<EntAreaUserPagingRes>> userPaging(@RequestBody PageItemDTO<EntAreaUserPagingReq> req) {
        return RR.success(entAreaBizService.entAreaUserPoPaging(req));
    }

    /**
     * 区域名单-分页-区域角度
     */
    @PostMapping("/user/tree/list")
    @RequiresPermissions("ent:area:paging")
    public RR<List<EntAreaDetailForSaleRes>> userTreeList(@RequestBody EntAreaDetailReq req) {
        return RR.success(entAreaBizService.listAreaDetailForSale(req));
    }

    /**
     * 区域名单-分配区域
     */
    @SysLog("区域名单-分配区域收回")
    @PutMapping("/user/config/delete")
    @RequiresPermissions("ent:area:config")
    public RR<Void> deleteUserConfig(@RequestBody EntAreaUserConfigReq req) {
        return entAreaBizService.deleteEntAreaUserConfig(req) ? RR.success() : RR.fail();
    }

    /**
     * 区域名单-分配区域
     */
    @SysLog("区域名单-分配区域")
    @PutMapping("/user/config")
    @RequiresPermissions("ent:area:config")
    public RR<Void> userConfig(@RequestBody EntAreaUserConfigReq req) {
        return entAreaBizService.entAreaUserConfig(req) ? RR.success() : RR.fail();
    }

    /**
     * 企业区域列表树形
     */
    @PostMapping("/tree")
    public RR<List<EntAreaTreeRes>> tree(@RequestBody EntAreaTreeReq req) {
        return RR.success(entAreaBizService.entAreaTree(req));
    }

    /**
     * 企业区域列表
     */
    @PostMapping("/list")
    public RR<List<EntAreaListRes>> list(@RequestBody EntAreaListReq req) {
        return RR.success(entAreaBizService.entAreaList(req));
    }

    /**
     * 企业区域树型结构
     */
    @PostMapping("/tree/list")
    @RequiresPermissions("ent:area:paging")
    public RR<List<EntAreaDetailRes>> treeList(@RequestBody EntAreaDetailReq req) {
        return RR.success(entAreaBizService.listEntAreaDetailRList(req));
    }

    /**
     * 企业区域分页
     */
    @PostMapping("/paging")
    @RequiresPermissions("ent:area:paging")
    public RR<PageItemVO<EntAreaPagingRes>> paging(@RequestBody PageItemDTO<EntAreaPagingReq> req) {
        return RR.success(entAreaBizService.entAreaPaging(req));
    }

    /**
     * 企业区域修改
     */
    @SysLog("企业区域修改")
    @PutMapping()
    @RequiresPermissions("ent:area:update")
    public RR<Void> update(@RequestBody EntAreaUpdateReq req) {
        return entAreaBizService.areaUpdate(req) ? RR.success() : RR.fail();
    }

    /**
     * 企业区域删除
     */
    @SysLog("企业区域删除")
    @DeleteMapping("/{idList}")
    @RequiresPermissions("ent:area:delete")
    public RR<Void> delete(@PathVariable("idList") List<Long> idList) {
        return entAreaBizService.areaDelete(idList) ? RR.success() : RR.fail();
    }

    /**
     * 企业区域管理新增
     */
    @SysLog("企业区域新增")
    @PostMapping("/save")
    @RequiresPermissions("ent:area:save")
    public RR<Void> save(@RequestBody EntAreaSaveReq req) {
        return entAreaBizService.areaSave(req) ? RR.success() : RR.fail();
    }
}
